<template>
	<view class="Test-container">
		<!-- <view class="btns">
			<button type="primary" @click="getChannels">获取支付通道</button>
			<button type="primary" @click="restoreComplateRequest" v-if="iap">获取已购买商品</button>

			<button type="primary" @click="payinfos">00000</button>
		</view> -->

		<view class="list">
			<view class="item" v-for="i in product_list" :key="i.productid">
				<view class="title">{{ i.title }}</view>
				<view class="price">{{ i.price }}元</view>
				<button type="warn" @click="pay(i)">支付</button>
			</view>
		</view>
	</view>
</template>

<script>
//OpenVip.do
import { applepay_api } from '@/api/index';

export default {
	data() {
		return {
			iap: null, // 支付通道
			ids: ['10018', 'cjhy10030'], // 应用内购项目产品 ID 数组
			product_list: [
				{
					productid: '10018',
					checked: true,
					title: '普通会员',
					price: 30
				},
				{
					productid: 'cjhy10030',
					checked: true,
					title: '超级会员',
					price: 50
				}
			] // 应用内购项目列表
		};
	},
	onLoad() {
		var that = this;
		that.getChannels();
		//this.payinfos();
		//that.restoreComplateRequest();
	},
	methods: {
		payinfos() {
			var that = this;

			try {
				const useridd = uni.getStorageSync('user_infos');

				var openidinfo = useridd.openid;
				console.log('ipendi', openidinfo);

				//调用临时接口
				that.https({
					url: applepay_api,
					method: 'POST',
					data: {
						type: 1,
						openId: openidinfo
					},
					success: resy => {
						console.log('后台返回信息', resy);
						//that.cartshoptxt = resy;
						if (resy.result_code == 0) {
							console.log('开通成功了');
						} else {
							console.log('开通失败了');
						}
						uni.reLaunch({
							url: '../index/start'
						});
					},
					fail: err => {
						console.log(err);
					},
					complete: function(eeee) {
						//console.log(eeee,'com')
					}
				});
				console.log(useridd);
			} catch (e) {
				// error
			}
		},
		// 获取支付通道
		getChannels() {
			// #ifdef APP-PLUS
			plus.payment.getChannels(
				res => {
					let channel = res.find(i => i.id === 'appleiap');
					this.iap = channel ? channel : null;
					this.requestOrder();
				},
				function(e) {
					// plus.nativeUI.alert("获取支付通道失败，请稍后重试。", function() {}, "提示");
				}
			);
			// #endif
		},
		// 获取内购项目列表
		requestOrder() {
			// #ifdef APP-PLUS
			this.iap.requestOrder(
				this.ids,
				res => {
					this.product_list = res;
				},
				function(errormsg) {
					plus.nativeUI.alert('获取应用内购项目失败，请稍后重试。', function() {}, '提示');
				}
			);
			// #endif
		},
		// 调起支付
		pay(i) {
			var that = this;
			console.log("支付开始了")
			// #ifdef APP-PLUS
			uni.showLoading({
				title: '支付中请勿离开',
				mask: true
			})

			uni.requestPayment({
				provider: 'appleiap',

				orderInfo: {
					productid: i.productid
				},

				success: e => {
					//e：返回的参数
					uni.hideLoading();
					that.payinfos();

					//1.未了防止掉单，建议先订单数据保存到本地

					//2.请求客户服务器的业务接口，成功后删除，该本地数据

					
				},

				fail: e => {
					uni.hideLoading();
					console.log("zhifu shi bai")
					uni.showModal({
						content: '充值失败,请重试',

						showCancel: false
					});
				},

				complete: () => {
					uni.hideLoading();
				}
			});

			// plus.payment.request(
			// 	this.iap,
			// 	{
			// 		productid: i.productid,
			// 		optimize: true // 支付时设置 optimize: true
			// 	},
			// 	result => {
			// 		// 支付成功回调
			// 		console.log('plus.payment.request-success--------支付成功------------', JSON.stringify(results));

			// 		that.payinfos();

			// 	},
			// 	e => {
			// 		uni.hideLoading();
			// 		console.log('支付失败了！')
			// 		// 用户取消支付或者调起支付失败回调
			// 		//restoreComplateRequest();
			// 	}
			// );
			// #endif
		},
		// 获取已购的非消耗性商品和订阅商品
		restoreComplateRequest() {
			this.iap.restoreComplateRequest(
				{},
				results => {
					// results 格式为数组存放恢复的IAP商品交易信息对象 IAPTransaction，通用需将返回的支付凭证传给后端进行二次认证
					console.log('restoreComplateRequest-results---', JSON.stringify(results));

					// console.log(re.sults[0].)
					// if(results[0])
				},
				e => {
					// 错误回调
					console.log('restoreComplateRequest-results ---fail---', JSON.stringify(e));
				}
			);
		}
	}
};
</script>

<style lang="scss">
.Test-container {
	padding: 24rpx;

	.btns {
		button {
			margin-bottom: 24rpx;
		}
	}

	.list {
		margin-top: 24rpx;

		.item {
			margin-bottom: 24rpx;
			padding: 30rpx;
			border-radius: 24rpx;
			box-shadow: 0rpx 12rpx 36rpx 0rpx rgba(0, 0, 0, 0.04);
			background-color: #252526;

			.title {
				margin-bottom: 24rpx;
				font-size: 36rpx;
				font-weight: bold;
				color: #ffffff;
			}

			.price {
				margin-bottom: 24rpx;
				font-size: 36rpx;
				font-weight: bold;
				color: #ff2c4c;
			}
		}
	}
}
</style>
